import sys

from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeySequence, QPixmap, QFont
from PyQt5.QtWidgets import *

from dao.DBUtil import checkTableExits, createTable
from util import util
from view.TabView import TabView


class ApplicationMain(QWidget):
    def __init__(self):
        super().__init__()
        self.setObjectName('MainWindow')
        self.setStyleSheet(
            '''
            QWidget {
                /* font-family: "微软雅黑","Times New Roman", "Webdings", Times, serif; */
                font-size: 14px;
                background-color: #fff;
            }
            ''')
        self.initUI()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            # self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        # self.setCursor(QCursor(Qt.ArrowCursor))

    def getTopRitht(self):
        topRight = QWidget()
        # topRight.setStyleSheet("border: 1px solid red;")
        topRight.resize(388, 20)

        topRightLayout = QHBoxLayout()
        topRightLayout.setContentsMargins(0, 0, 0, 0)
        qlabel = QLabel()
        qlabel.resize(4, 4)
        # qlabel.setStyleSheet('width: 16px; height: 16px')
        pix = QPixmap('resources/e-do16.ico')  # 注意修改Windows路径问题
        qlabel.setPixmap(pix)
        qlabel.setScaledContents(True)
        topRightLayout.addWidget(qlabel)

        topRightLayout.addStretch(1)

        _translate = QtCore.QCoreApplication.translate
        font = QtGui.QFont()
        font.setFamily("Webdings")
        # 最小化按钮
        minBtn = QPushButton(self)
        minBtn.setObjectName("minBtn")
        # minBtn.setFont(font)
        # minBtn.setText('0')
        minBtn.setText(_translate("Form", "-"))
        minBtn.setStyleSheet('''
            QPushButton {
                border: none;
                margin-right: 5px;
                background: #5AD65D;
                border-radius: 3px;
                width: 22px;
                height: 22px;
                line-height: 22px;
                text-align: center;
                font-weight: 500;
            }
        ''')
        # minBtn.
        minBtn.clicked.connect(self.minEventMethod)

        closeBtn = QPushButton()
        closeBtn.setFont(font)
        closeBtn.setText('r')
        closeBtn.setStyleSheet('''
            QPushButton {
                border: none;
                background: #F25A5A;
                border-radius: 3px;
                width: 22px;
                height: 22px;
                line-height: 22px;
            }
        ''')
        closeBtn.clicked.connect(self.closeEventMethod)

        topRightLayout.addWidget(minBtn)
        topRightLayout.addWidget(closeBtn)
        topRight.setLayout(topRightLayout)

        return topRight

    def minEventMethod(self):
        self.showMinimized()

    def closeEventMethod(self):
        self.close()

    def initUI(self):
        self.resize(380, 437)
        self.setMinimumWidth(380)
        self.setMaximumWidth(380)
        self.setMinimumHeight(437)
        self.setMaximumHeight(437)
        # 透明度
        self.setWindowOpacity(0.9)
        # 无边框
        self.setWindowFlags(Qt.FramelessWindowHint)

        self.setWindowTitle("Todo")

        QShortcut(QKeySequence(self.tr("Alt+O")), self, self.maxWin)
        QShortcut(QKeySequence(self.tr("Alt+M")), self, self.showMinimized)

        self.qtab = QTabWidget()

        date_list = util.get_week(util.get_today())
        for item in date_list:
            tab = TabView(item['value'])
            self.qtab.addTab(tab, item['name'])

        self.layout = QVBoxLayout()

        self.topRight = self.getTopRitht()

        self.layout.addWidget(self.topRight)
        self.layout.addWidget(self.qtab)
        self.setLayout(self.layout)
        self.show()

    def maxWin(self):
        print("最大化")
        if self.isMaximized():
            self.showNormal()
        else:
            self.showMaximized()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    a = ApplicationMain()

    res = checkTableExits()
    print(res)
    if not res:
        createTable()

    sys.exit(app.exec_())
